/*
 * 功能：自定义模板
 */

(function(factory){
    if(typeof define === 'function' && define.amd){
        define(['jquery'],factory);
    }else{
        factory(window.jQuery);
    }
}(function($){
    $.fn.tpl = function(o) {

        return this.each(function() {
            var t = $(this);
            if(t.length==0)return false;
            var opts = $.extend({},$.fn.tpl.defaults, o);
            var createtable=function(){
                var tpllist=t;
                tpllist.html('');
                var th=['费用分类','费用项','收费机构','金额','收费时间/阶段','退款标准'];
                var html=[];
                html.push('<table class="table table-border"><thead><tr>');
                $.each(opts.a,function(i,j) {
                    html.push('<th>' + j.th + '</th>');
                });
                html.push('</tr></thead>');
                html.push('<tbody>');
                var tr='';
                var specdata=opts.data;
                $.each(opts.item,function(i,j){
                    if($.inArray(i,specdata.aid)>=0){
                        //console.log(j.info.value);
                        var _item=[i, j.info.name];

                        var sub=j.info.value;
                        var name=_item[1];
                        var num=0;
                        if(j.info.valueNum>1) {
                            $.each(sub, function (l, m) {
                                if ($.inArray(m.id, specdata.bid) >= 0) {
                                    num++;
                                }
                            });
                        }

                        var rowspan=j.info.valueNum>1?'rowspan="'+num+'"':'';
                        if(j.info.valueNum>1){
                            $.each(sub,function (l,m) {
                                if($.inArray(m.id,specdata.bid)>=0) {
                                    var sname = m.value;
                                    var _sitem = [m.id, m.value];

                                    tr += '<tr id="item_'+_item[0]+'_'+_sitem[0]+'" data-id="'+_item[0]+'_'+_sitem[0]+'">';
                                    if (l==0){
                                        tr += '<td ' + rowspan + '>' + name + '</td>';
                                    }
                                    $.each(opts.a,function(i,j) {
                                        if(i!=0){
                                            tr+='<td id="'+ j.id+'">' + getTd(j,sname,_item[0]+'_'+_sitem[0]) + '</td>';
                                        }
                                    });
                                    tr += '</tr>';
                                }
                            });
                        }else{
                            tr+=getli(name,_item);
                        }
                    }
                });

                html.push(tr);
                html.push('</tbody></table>');

                tpllist.html(html.join(''));
            }


            var getli=function(name,_item){
                var tr='';
                tr+='<tr id="item_'+_item[0]+'" data-id="'+_item[0]+'">';
                $.each(opts.a,function(i,j) {
                    tr+='<td id="'+ j.id+'">' + getTd(j,name,_item[0]) + '</td>';
                });
                tr+='</tr>';

                return tr;
            };
            var getTd=function(j,name,id){
                var str='';
                switch(j.type){
                    case 'select':
                        var item=j.item;
                        var sv=getv(id, j.id);
                        for(var i=0;i<item.length;i++){
                            if(sv==item[i][0]){
                                return item[i][1];
                            }
                        }
                        break;
                    case 'input':
                        str=getv(id, j.id);
                        break;
                    default :
                        str=name;
                }
                return str;
            }
            var getv=function(row,col){
                var specdata=opts.data;
                var v='';
                $.each(specdata.val,function(i,j){
                    if(j.row==row && j.col==col){
                        v=j.v
                        return ;
                    }
                });
                return v;
            }
            createtable();
        });
    };
    $.fn.tpl.defaults = {
        a:[
            {id:1,th:'费用分类',type:''},
            {id:2,th:'费用项',type:''},
            {id:3,th:'收费机构',type:'select',width:110,item:[[0,'请选择'],[1,'本公司'],[2,'移民局'],[3,'律师'],[4,'相关机构'],[5,'国家签证中心'],[6,'项目方'],[7,'医院']]},
            {id:4,th:'金额',type:'input',width:110},
            {id:5,th:'收费时间/阶段',type:'select',width:110,item:[[1,'签约'],[2,'递交申请前支付']]},
            {id:6,th:'退款标准',type:'select',width:110,item:[[1,'可退'],[2,'不可退']]}
        ]
    };
}));